<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>倒计时</title>
    <style>
        #time {
            margin: 12% auto auto;
            width: 200px;
        }
    </style>
</head>
<body>
    <p id="time"></p>
    <script>
        const timer = setInterval(function(){
            timeLeft("time",new Date("2018-09-21T18:00:00+08:00"));
        },1);

        function timeLeft(select, endTime){//curTime默认为当下时间
            let leftTime = new Date(endTime - Date.now());
            if(leftTime <= 0){//如果时间差小于等于0
                clearInterval(timer);//清除定时器
                document.getElementById(select).innerHTML = 0+"天"+0+"时"+0+"分"+0+"秒"+0+"毫秒";
            }else{
                let days = Math.floor(leftTime / (1000 * 60 * 60 * 24));//天
                let hours = leftTime.getUTCHours();
                let mi = leftTime.getMinutes();//分
                let se = leftTime.getSeconds();//秒
                let ms = leftTime.getMilliseconds();//毫秒
                document.getElementById(select).innerHTML = days+"天"+hours+"时"+mi+"分"+se+"秒"+ms+"毫秒";
            }
        }
    </script>
</body>
</html>